package com.xzit.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzit.rental.entity.AutoMaker;
import com.xzit.rental.service.IAutoMakerService;
import com.xzit.rental.utils.PinYinUtils;
import com.xzit.rental.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  车辆厂商前端控制器
 * </p>
 *
 * @author 何时说晚安
 * @since 2024-12-05
 */
@RestController
@RequestMapping("/rental/autoMaker")
public class AutoMakerController {

    @Resource
    private IAutoMakerService autoMakerService;

    /**
     * 分页查询厂商记录
     * @param start 当前起始页
     * @param size 一页的记录数
     * @param autoMaker 查询对象
     * @return
     */
    @PostMapping("/{start}/{size}")
    public Result search(@PathVariable int start,
                         @PathVariable int size,
                         @RequestBody AutoMaker autoMaker) {
        Page<AutoMaker> page = autoMakerService.search(start, size, autoMaker);
        return Result.success(page).setMessage("分页查询成功");
    }

    /**
     * 按照给定ids删除车辆厂商记录
     * @param ids 给定的ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        List<Integer> list = Arrays.stream(ids.split(",")).map(Integer::parseInt).toList();
        return autoMakerService.removeBatchByIds(list)?Result.success():Result.fail();
    }

    /**
     * 新增厂商记录
     * @param autoMaker 提供的厂商对象
     * @return
     */
    @PostMapping("")
    public Result save(@RequestBody AutoMaker autoMaker) {
        autoMaker.setOrderLetter(PinYinUtils.getPinYin(autoMaker.getName()));
        return autoMakerService.save(autoMaker)?Result.success():Result.fail();
    }

    /**
     * 修改厂商记录
     * @param autoMaker 提供的厂商对象
     * @return
     */
    @PutMapping("")
    public Result update(@RequestBody AutoMaker autoMaker) {
        autoMaker.setOrderLetter(PinYinUtils.getPinYin(autoMaker.getName()));
        return autoMakerService.updateById(autoMaker)?Result.success():Result.fail();
    }

    @GetMapping("")
    public Result selectAll(){
        return Result.success().setData(autoMakerService.list());
    }

}
